package bigo.todolist.avocado;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.util.Log;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;

/** 
 * Set up the add a task map activity map
 * @authorAlex Irlik, Steve Fox, TiOluwa Olarewaju, Derek Miller
 * Big-Ovacado Task Force
 * Version 1.0
 */

public class MapsActivity extends MapActivity{

	BigMapView mapView;
	MapController mc;
	List<Overlay> listOfOverLays;
	BigMapView.OnLongpressListener listen;
	

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
        
        mapView = (BigMapView) findViewById(R.id.mapview1);
        mapView.setBuiltInZoomControls(true);
        mc = mapView.getController();
        
        listen = new BigMapView.OnLongpressListener() {
        	private GeoPoint location;
        	
        	/**
        	 * A listener for the longpress on the map
        	 */
            public void onLongpress(final MapView view, final GeoPoint longpressLocation) {
                    
                        // Insert your longpress action here
                    	location = longpressLocation;
                    	Integer latitude = longpressLocation.getLatitudeE6();//Get and prepare coordinates
                    	Integer longitude = longpressLocation.getLongitudeE6();
                    	latitude = (int) (latitude / (1E6));
                    	longitude = (int) (longitude / (1E6));
                    	addOverlays(longpressLocation); //Add point to overlays
                    	
                    	Intent coordinates = new Intent(); //Prepare a send a bundle back to add task. Bundle contains coordinates
                    	Bundle bundle = new Bundle();
                    	bundle.putDouble("latitude", latitude);
                    	bundle.putDouble("longitude",longitude);
                    	coordinates.putExtras(bundle);
                    	setResult(RESULT_OK,coordinates);
                    	finish();
                    	
                    	
                }
            /**
             * A get location method in case we need it
             * @return
             */
            public GeoPoint getLocation(){
            	return location;
            }
            };
        
        mapView.setOnLongpressListener(listen); //Set listener on map
        
        String coordinates[] = {"33", "-84"}; //Set starting coordinates
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        GeoPoint p = new GeoPoint(
        (int) (lat * 1E6),
        (int) (lng * 1E6));

        mc.animateTo(p); //Go to default coordinates
        mc.setZoom(11); //Set zoom
        mapView.invalidate();
    }

    /**
     *  A routing method
     */
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	/**
	 * Placing points in the map
	 * @param p
	 */
	public void addOverlays(GeoPoint p){
		listOfOverLays = mapView.getOverlays();
    	GeoPoint d = p;
    	Drawable drawable = getResources().getDrawable(R.drawable.ex);
    	BigItemizedOverlay itemizedoverlay = new BigItemizedOverlay(drawable, this);
    	OverlayItem overlayitem = new OverlayItem(p, "Test Task", "Test Description");
    	itemizedoverlay.addOverlay(overlayitem);
    	listOfOverLays.add(itemizedoverlay);//Add point to map	
	}
	
	public boolean testAddOverlays(GeoPoint testPoint){ //Test to make sure points are added
		GeoPoint test = testPoint;
		if (test != null){
			Log.w("Taskmaster", "False check");
			addOverlays(test);
		}
		
		return !listOfOverLays.isEmpty();	
	}
	
	
}